﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QLNH_DAL_WS
{
    public class TableDAO
    {
        public List<TableDTO> GetAllTable()
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var result = from table in data.TABLEs
                         select table;

            List<TableDTO> retVal = new List<TableDTO>();

            foreach (TABLE table in result)
            {
                retVal.Add(new TableDTO(table.ID, Int32.Parse(table.Capacity.ToString())));
            }

            return retVal;
        }

        public bool AddTable(int capacity)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var lastID = from table in data.TABLEs
                          orderby table.ID descending
                          select table.ID;

            var tabCode = (from code in data.CODEs
                           where code.Data.Equals("table")
                           select code).First();

            string tableID = tabCode.Prefix;

            if (lastID.Count() > 0)
            {
                string tmp = lastID.First();
                tableID = Ultilities.GetNextId(tmp, "table");
            }
            else
            {
                for (int i = 0; i < tabCode.PostfixLength; i++)
                {
                    tableID += "0";
                }
            }

            try
            {
                TABLE table = new TABLE();
                table.ID = tableID;
                table.Capacity = capacity;

                data.TABLEs.InsertOnSubmit(table);
                data.SubmitChanges(System.Data.Linq.ConflictMode.FailOnFirstConflict);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool EditTable(TableDTO tableDTO)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var result = from table in data.TABLEs
                         where table.ID.Equals(tableDTO.ID)
                         select table;

            if (result.Count() > 0)
            {
                result.First().Capacity = tableDTO.Capacity;
                try
                {
                    data.SubmitChanges(System.Data.Linq.ConflictMode.FailOnFirstConflict);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            else
                return false;
        }

        public bool RemoveTable(string id)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var result = from table in data.TABLEs
                         where table.ID.Equals(id)
                         select table;
            if (result.Count() > 0)
            {
                try
                {
                    data.TABLEs.DeleteOnSubmit(result.First());
                    data.SubmitChanges(System.Data.Linq.ConflictMode.FailOnFirstConflict);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            else
                return false;
        }

        public List<TableDTO> GetAllEmptyTable()
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            List<TableDTO> result = new List<TableDTO>();

            List<string> notEmptyTables = (from o in data.ORDERs
                                           select o.TableID).Distinct().ToList<string>();

            result = (from t in data.TABLEs
                        select new TableDTO()
                        {
                            ID = t.ID,
                            Capacity = Int32.Parse(t.Capacity.ToString())
                        }).ToList<TableDTO>();


            for (int i = 0; i < notEmptyTables.Count(); i++)
            {
                result.Remove(result.Find(r => r.ID.Equals((string)notEmptyTables[i])));
            }

            return result;
        }
    }
}